home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / DirectShow / DMO / DMOSample / state.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-08  |  3.5 KB  |  138 lines

  1. //------------------------------------------------------------------------------
  2. // File: State.cpp
  3. //
  4. // Desc: DirectShow sample code - implementation of CStreamState class.
  5. //
  6. // Copyright (c) 2000-2001 Microsoft Corporation.  All rights reserved.
  7. //------------------------------------------------------------------------------
  8.  
  9.  
  10. #include "stdafx.h"
  11. #include <limits.h>     //  _I64_MAX
  12. #include <crtdbg.h>
  13. #include <dmo.h>
  14. #include "state.h"
  15.  
  16. void CStreamState::TimeStamp(REFERENCE_TIME rt)
  17. {
  18.     DWORD dwIndex = m_cbBytes >= 4 ? 0 : m_cbBytes;
  19.     m_arTS[dwIndex].bValid = true;
  20.     m_arTS[dwIndex].rt = rt;
  21. }
  22.  
  23. void CStreamState::Reset()
  24. {
  25.     m_cbBytes = 0;
  26.     m_dwNextTimeCode = 0;
  27.     for (int i = 0; i < 4; i++) {
  28.         m_arTS[i].bValid = false;
  29.     }
  30. }
  31.  
  32. bool CStreamState::NextByte(BYTE bData)
  33. {
  34.     _ASSERTE(m_arTS[0].bValid);
  35.     switch (m_cbBytes) {
  36.     case 0:
  37.         if (bData == 0) {
  38.             m_cbBytes++;
  39.         }
  40.         return false;
  41.     case 1:
  42.         if (bData == 0) {
  43.             m_cbBytes++;
  44.         } else {
  45.             m_cbBytes = 0;
  46.  
  47.             //  Pick up new timestamp if there was one
  48.             if (m_arTS[1].bValid) {
  49.                 m_arTS[0].rt = m_arTS[1].rt;
  50.                 m_arTS[1].bValid = false;
  51.             }
  52.         }
  53.         return false;
  54.     case 2:
  55.         if (bData == 1) {
  56.             m_cbBytes++;
  57.         } else {
  58.             if (bData == 0) {
  59.                if (m_arTS[1].bValid) {
  60.                    m_arTS[0].rt = m_arTS[1].rt;
  61.                    m_arTS[1].bValid = false;
  62.                }
  63.                if (m_arTS[2].bValid) {
  64.                    m_arTS[1] = m_arTS[2];
  65.                    m_arTS[2].bValid = false;
  66.                }
  67.             } else {
  68.                 //  Not 0 or 1, revert
  69.                 m_cbBytes = 0;
  70.                 //  and pick up latest timestamp
  71.                 if (m_arTS[1].bValid) {
  72.                     m_arTS[0].rt = m_arTS[1].rt;
  73.                     m_arTS[1].bValid = false;
  74.                 }
  75.                 if (m_arTS[2].bValid) {
  76.                     m_arTS[0].rt = m_arTS[2].rt;
  77.                     m_arTS[2].bValid = false;
  78.                 }
  79.             }
  80.         }
  81.         return false;
  82.     case 3:
  83.     {
  84.         //  It's a start code whatever it is
  85.         //  return the timestamp and reset everything
  86.         m_rt = m_arTS[0].rt;
  87.  
  88.         //  If it's a picture start code can't use this timestamp again.
  89.         if (0 == bData) {
  90.             m_arTS[0].rt = INVALID_TIME;
  91.             m_cbBytes = 0;
  92.         }
  93.  
  94.         //  Catch up and clean out timestamps
  95.         for (int i = 1; i < 4; i++) {
  96.             if (m_arTS[i].bValid) {
  97.                 m_arTS[0].rt = m_arTS[i].rt;
  98.                 m_arTS[i].bValid = false;
  99.             }
  100.         }
  101.  
  102.         // Picture start code?
  103.         if (0 == bData) {
  104.             m_cbBytes = 0;
  105.             m_dwTimeCode = m_dwNextTimeCode;
  106.             m_dwNextTimeCode++;
  107.             return true;
  108.         } else {
  109.             //  Is it a GOP start code?
  110.             if (bData == 0xb8) {
  111.                 m_cbBytes++;
  112.             } else {
  113.                 m_cbBytes = 0;
  114.             }
  115.             return false;
  116.         }
  117.     }
  118.  
  119.     case 4:
  120.     case 5:
  121.     case 6:
  122.     case 7:
  123.         m_bGOPData[m_cbBytes - 4] = bData;
  124.         m_cbBytes++;
  125.         if (m_cbBytes == 8) {
  126.             m_cbBytes = 0;
  127.             m_dwNextTimeCode = ((DWORD)m_bGOPData[0] << 17) +
  128.                                ((DWORD)m_bGOPData[1] << 9) +
  129.                                ((DWORD)m_bGOPData[2] << 1) +
  130.                                ((DWORD)m_bGOPData[3] >> 7);
  131.         }
  132.         return false;
  133.     }
  134.  
  135.     // Should never reach here
  136.     return false;
  137. };
  138.